const Storage = require('./storage').default

let engine
let cache = []
let loggerKey = "logger"

function format(v, level = 'error') {
  let d = new Date()
  if (level === 'level') {
    return `[ERROR] ${d} ${v}`
  }
  return `[OTHER] ${d} ${v}`
}

function init() {
  engine = new Storage(wx);
  cache = engine.get(loggerKey) || []
}

function set(k, v, level) {
  cache.push({
    key: k,
    level,
    data: format(v, level)
  })
  engine.set(loggerKey, cache)
}

function get(k) {
  return cache[k]
}

export default {
  init,
  get,
  set
}
